package org.kazuhiko.rpg.server.net.packet.handler.handlers;

import org.kazuhiko.rpg.server.Server;
import org.kazuhiko.rpg.server.net.RPGSession;
import org.kazuhiko.rpg.server.net.packet.Packet;
import org.kazuhiko.rpg.server.net.packet.handler.PacketHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class AuthenticationPacketHandler implements PacketHandler<RPGSession>
{
	private static final Logger logger = LoggerFactory.getLogger( AuthenticationPacketHandler.class );
	
	@Override
	public void handlePacket( RPGSession session, Packet packet )
	{
		//Already authenticated
		if( session.isAuthenticated() )
		{
			return;
		}
		
		//Yes this is not very safe
		String username = packet.getStringField( "username" );
		String password = packet.getStringField( "key" );
		
		logger.info( "Received authentication packet : username=" + username + ", key=" + password );
		
		int loginResponse = Server.getServer().register( session, username );
		
		if( loginResponse == 0 )
		{
			//We are logged in
		}
		else if( loginResponse == 1 )
		{
			//To many users online
			//send message too many users online
		}
		else if ( loginResponse == 2 )
		{
			//Duplicated username
			//send message that the user is already logged in
		}
	}
}
